﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

public class Category : SingleWindow<Category> 
{
    UIGrid FamilyGrid;

    UIGrid GenusGrid;

    GameObject FamilyPrefab;

    GameObject GenusPrefab;

    UIButton Return;
    
    public class ButtonInfo
    {
        public string iconName;
        public bool result;
    }

    public class GenusInfo
    {
        public string name;
        public string iconName;
        public int resultIndex;
        public string resultSoundName;
        public List<ButtonInfo> butInfoList = new List<ButtonInfo>();
    }

    public class FamilyInfo
    {
        public string name;
        public string iconName;
        public List<GenusInfo> genusInfoList = new List<GenusInfo>();
    }

    List<FamilyInfo> fInfoList = null;

    void Start() 
    {
        Return = Util.Find<UIButton>(gameObject, "Return");
        UIEventListener.Get(Return).onClick += OnReturn;

        FamilyGrid = Util.Find<UIGrid>(gameObject, "FamilyGrid");

        GenusGrid = Util.Find<UIGrid>(gameObject, "GenusGrid");

        FamilyPrefab = Util.Find(gameObject, "FamilyPrefab");
        FamilyPrefab.SetActive(false);

        GenusPrefab = Util.Find(gameObject, "GenusPrefab");
        GenusPrefab.SetActive(false);

        TextAsset ta = Resources.Load("config") as TextAsset;
        Debug.Log(ta);

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(ta.text);

        XmlNode cn = doc.SelectSingleNode("config");
        if(cn != null)
        {
            fInfoList = new List<FamilyInfo>();
            XmlNodeList fns = cn.SelectNodes("family");
            foreach (XmlNode fn in fns)
            {
                if (fn.NodeType != XmlNodeType.Element)
                    continue;

                FamilyInfo fInfo = new FamilyInfo();
                XmlElement fe = (XmlElement)fn;
                fInfo.name  = fe.GetAttribute("name");
                fInfo.iconName = fe.GetAttribute("icon");

                fInfoList.Add(fInfo);

                GameObject familyGroupGO = NGUITools.AddChild(FamilyGrid.gameObject, FamilyPrefab);
                GameObject fBut = Util.Find(familyGroupGO, "Button");
                UIEventListener.Get(fBut).onClick += OnFamlilyButton;
                CustomData.Set(fBut, fInfo);

                UITexture fTex = Util.Find<UITexture>(fBut, "Texture");
                fTex.mainTexture = Resources.Load(fInfo.iconName) as Texture;

                UILabel label = Util.Find<UILabel>(fBut, "Label");
                label.text = fInfo.name;

                XmlNodeList gns = fn.SelectNodes("genus");
                foreach (XmlNode gn in gns)
                {
                    Debug.Log(gn);

                    if (gn.NodeType != XmlNodeType.Element)
                        continue;

                    GenusInfo gInfo = new GenusInfo();
                    XmlElement ge = (XmlElement)gn;
                    gInfo.name = ge.GetAttribute("name");
                    gInfo.iconName = ge.GetAttribute("icon");
                    gInfo.resultIndex = int.Parse(ge.GetAttribute("resultIndex"));
                    gInfo.resultSoundName = ge.GetAttribute("resultSound");

                    fInfo.genusInfoList.Add(gInfo);

                    XmlNodeList bns = gn.SelectNodes("button");
                    foreach (XmlNode bn in bns)
                    {
                        if (bn.NodeType != XmlNodeType.Element)
                            continue;

                        ButtonInfo bInfo = new ButtonInfo();
                        XmlElement be = (XmlElement)bn;
                        bInfo.iconName = be.GetAttribute("icon");
                        bInfo.result = be.GetAttribute("icon") == "1" ? true : false;
                        gInfo.butInfoList.Add(bInfo);
                    }
                }
                //CustomData.Set()
            }
            FamilyGrid.Reposition();
        }

	}

    public override void OnShow()
    {
        base.OnShow();


    }

    void OnReturn(GameObject go)
    {
        Hide();

        StartMenu.Instance.Show();
    }

    void OnFamlilyButton(GameObject go)
    {
        FamilyInfo fInfo = CustomData.Get(go) as FamilyInfo;
        if (fInfo == null)
            return;

        Util.DestroyAllChildren(GenusGrid);

        Debug.Log(fInfo.genusInfoList.Count);
        foreach (GenusInfo gInfo in fInfo.genusInfoList)
        {
            GameObject GenusGroupGO = NGUITools.AddChild(GenusGrid.gameObject, GenusPrefab);
            GameObject gBut = Util.Find(GenusGroupGO, "Button");
            UIEventListener.Get(gBut).onClick += OnGenusButton;
            CustomData.Set(gBut, gInfo);

            UITexture gTex = Util.Find<UITexture>(gBut, "Texture");
            gTex.mainTexture = Resources.Load(gInfo.iconName) as Texture;

            UILabel label = Util.Find<UILabel>(gBut, "Label");
            label.text = gInfo.name;
        }
        GenusGrid.Reposition();
        
    }

    void OnGenusButton(GameObject go)
    {
        GenusInfo gInfo = CustomData.Get(go) as GenusInfo;
        if (gInfo == null)
            return;


    }


}
